package com.gamecollection.minesweeper.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.Objects;

/**
 * 扫雷游戏工具类
 */
public class GameUtils {
    
    // 游戏难度配置
    public static final int EASY_MINES = 10;
    public static final int EASY_WIDTH = 9;
    public static final int EASY_HEIGHT = 9;
    
    public static final int MEDIUM_MINES = 40;
    public static final int MEDIUM_WIDTH = 16;
    public static final int MEDIUM_HEIGHT = 16;
    
    public static final int HARD_MINES = 99;
    public static final int HARD_WIDTH = 30;
    public static final int HARD_HEIGHT = 16;
    
    // 游戏常量
    public static final int OFFSET = 45;
    public static final int SQUARE_LENGTH = 50;
    
    // 游戏状态
    public static final int STATE_SELECT_LEVEL = 0;
    public static final int STATE_PLAYING = 1;
    public static final int STATE_WIN = 2;
    public static final int STATE_GAME_OVER = 3;
    
    // 图片资源
    public static Image lei;
    public static Image top;
    public static Image flag;
    public static Image noflag;
    public static Image face;
    public static Image over;
    public static Image win;
    public static Image[] numberImages = new Image[9];
    
    // 静态初始化块
    static {
        try {
            lei = loadImage("/img/saolei/lei.png");
            top = loadImage("/img/saolei/top.gif");
            flag = loadImage("/img/saolei/flag.gif");
            noflag = loadImage("/img/saolei/noflag.png");
            face = loadImage("/img/saolei/face.png");
            over = loadImage("/img/saolei/over.png");
            win = loadImage("/img/saolei/win.png");
            
            for (int i = 1; i <= 8; i++) {
                numberImages[i] = loadImage("/img/saolei/num/" + i + ".png");
            }
        } catch (Exception e) {
            System.err.println("Failed to load minesweeper images: " + e.getMessage());
            // 创建默认图片
            lei = createDefaultImage(Color.BLACK, 50, 50);
            top = createDefaultImage(Color.GRAY, 50, 50);
            flag = createDefaultImage(Color.RED, 50, 50);
            noflag = createDefaultImage(Color.ORANGE, 50, 50);
            face = createDefaultImage(Color.YELLOW, 50, 50);
            over = createDefaultImage(Color.RED, 50, 50);
            win = createDefaultImage(Color.GREEN, 50, 50);
            
            for (int i = 1; i <= 8; i++) {
                numberImages[i] = createDefaultImage(Color.BLUE, 50, 50);
            }
        }
    }
    
    /**
     * 绘制文字的工具类
     */
    public static void drawWord(Graphics g, String str, int x, int y, Color color) {
        g.setColor(color);
        g.setFont(new Font("仿宋", Font.BOLD, 30));
        g.drawString(str, x, y);
    }
    
    /**
     * 加载图片
     */
    public static Image loadImage(String path) {
        try {
            Image img = ImageIO.read(Objects.requireNonNull(GameUtils.class.getResource(path)));
            if (img == null) {
                System.out.println("图片加载失败: " + path);
                return createDefaultImage(Color.GRAY, 50, 50);
            }
            return img;
        } catch (Exception e) {
            System.out.println("图片加载失败: " + path);
            return createDefaultImage(Color.GRAY, 50, 50);
        }
    }
    
    /**
     * 创建默认图片
     */
    private static Image createDefaultImage(Color color, int width, int height) {
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setColor(color);
        g2d.fillRect(0, 0, width, height);
        g2d.dispose();
        return img;
    }
}
